#include "FIleFilter.h"
#include "FileControl.cpp"

FIleFilter::FIleFilter() {}

FIleFilter *FIleFilter::shared()
{
    return new FIleFilter();
}

bool FIleFilter::filter(const fs::path &path)
{
    if (None != fSide && !SizeFilter(path))
    {
        return false;
    }
    // 判断 regex 不是空值
    if (bNameFilterStatus && !NameFilter(path))
    {
        return false;
    }
    return true;
}

void FIleFilter::setSize(ulint size,FILESIDE fside){
    fileSize = size;
    this->fSide = fside;
}

bool FIleFilter::SizeFilter(const fs::path &path)
{
    ulint size = SizeOfPath(path);
    switch (fSide)
    {
    case Great:
    {
        if (size > fileSize)
            return true;
        break;
    }
    case Less:
    {
        if (size < fileSize)
            return true;
        break;
    }
    case GreatAndEqual:
    {
        if (size >= fileSize)
            return true;
        break;
    }
    case LessAndEqual:
    {
        if (size <= fileSize)
            return true;
        break;
    }
    case Equal:
    {
        if (size == fileSize)
            return true;
        break;
    }
    case None:
        break;
    }
    return false;
}

void FIleFilter::setRegex(const std::string &str, bool bCase)
{
    if(str.empty()){
        return;
    }
    // 忽略大小写
    try
    {
        if (bCase)
        {
            regex = std::regex(str, std::regex_constants::icase);
        }
        else
        {
            regex = std::regex(str);
        }
        bNameFilterStatus = true;
    }catch(const std::regex_error& e){
        Terminal_Warn<< e.what();
    }
  
}
bool FIleFilter::NameFilter(const fs::path &path)
{
    std::string fileName = path.filename().string();
    if (std::regex_match(fileName, regex))
    {
        return true;
    }
    else
    {
        return false;
    }
}
